<?php

include 'def.php';

header( 'content-type: application/json; charset=utf-8' );

$json_array = array( 'status'=>'error', 'error_id'=>'input', 'error_description'=>'request data error' );

session_start();

if( empty( $_POST ) )
{ /*do nothing*/ 
}
elseif( !isset($_POST['studentid']) || !isset($_POST['teacherid']) || !isset($_POST['classid']) || !isset($_POST['data']) )
{/*do nothing*/
}
else{
	$studentid = $_POST['studentid'];
	$teacherid = $_POST['teacherid'];
	$classid   = $_POST['classid'];
	$data      = $_POST['data'];
	
	/*need to push in all scripts*/
	if( !isValidMd5($studentid) || !isValidMd5($studentid) || !isValidMd5($classid) )
	{
		/*do nothing*/
	}
	else{
		$json = json_decode( $data, true );
		if( json_last_error() != JSON_ERROR_NONE )
		{/* do nothing */
		}
		else{
			$conn = mysql_connect( DBSERV, DBUSER, DBPASSWD ) or die( "Couldn't connect to database" );
			$db = mysql_select_db( DB ) or die( "Couldn't connect to database" );
			
			$query = "select id from rubric rc Inner Join class cs on rc.classid=cs.classid where rc.studentid='$studentid' and rc.classid='$classid'and cs.teacherid='$teacherid';";
	
			$result = mysql_query( $query ) or die( 'Query_failed: ' . mysql_error() );
			
			/*unique id returns one row*/
			if( mysql_num_rows($result) != 1 )
			{
				$json_array = array( 'status'=>'error', 'error_id'=>'$id','error_description'=>'data no found' );
			}
			else{
				$stuID = $json['studentid'];
				$stuType = $json['type'];
				$id = $result['id'];
								
				$update = "update rubric set studentid='$stuID' and type=$stuType where id=$id";
				
				$r = mysql_query( $update ) or die('Query_failed: '. mysql_error());
				
				if( $r )
				{
					$tbs = array('rubricgradeline', 'rubricgradelevel', 'rubricSection');
					$json_array = updateTables( $tbs, 'rubricid', $id, $json);
				}	
				
			}
			
			
			
			}
		}
	}
		
echo( json_encode($json_array) );

?>